package com.example.springboot.utils.word;

import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;

public class WordFactory {

	/**
	 * TODO 得到单元格第一个Paragraph
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 */
	public static XWPFParagraph getCellFirstParagraph(XWPFTableCell cell) {
		XWPFParagraph p;
		if (cell.getParagraphs() != null && cell.getParagraphs().size() > 0) {
			p = cell.getParagraphs().get(0);
		} else {
			p = cell.addParagraph();
		}
		return p;
	}

	/**
	 * TODO 获取段落中的文本域
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 * @param isInsert
	 *            是否是新增
	 * 
	 * @param isNewLine
	 *            是否新行
	 */
	public static XWPFRun getOrAddParagraphFirstRun(XWPFParagraph paragraph, boolean isInsert, boolean isNewLine) {
		XWPFRun pRun = null;
		if (isInsert) {
			pRun = paragraph.createRun();
		} else {
			if (paragraph.getRuns() != null && paragraph.getRuns().size() > 0) {
				pRun = paragraph.getRuns().get(0);
			} else {
				pRun = paragraph.createRun();
			}
		}
		if (isNewLine) {
			pRun.addBreak();
		}
		return pRun;
	}

	/**
	 * TODO 得到段落CTPPr
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 */
	public static CTPPr getParagraphCTPPr(XWPFParagraph paragraph) {
		CTPPr pPPr = null;
		if (paragraph.getCTP() != null) {
			if (paragraph.getCTP().getPPr() != null) {
				pPPr = paragraph.getCTP().getPPr();
			} else {
				pPPr = paragraph.getCTP().addNewPPr();
			}
		}
		return pPPr;
	}

	/**
	 * TODO 得到XWPFRun的CTRPr
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 */
	public static CTRPr getRunCTRPr(XWPFParagraph paragraph, XWPFRun pRun) {
		CTRPr pRpr = null;
		if (pRun.getCTR() != null) {
			pRpr = pRun.getCTR().getRPr();
			if (pRpr == null) {
				pRpr = pRun.getCTR().addNewRPr();
			}
		} else {
			pRpr = paragraph.getCTP().addNewR().addNewRPr();
		}
		return pRpr;
	}

	/**
	 * TODO 得到Table的CTTblPr,不存在则新建
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 */
	public static CTTblPr getTableCTTblPr(XWPFTable table) {
		CTTbl ttbl = table.getCTTbl();
		CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
		return tblPr;
	}

	/**
	 * TODO 得到Cell的CTTcPr,不存在则新建
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 */
	public static CTTcPr getCellCTTcPr(XWPFTableCell cell) {
		CTTc cttc = cell.getCTTc();
		CTTcPr tcPr = cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr();
		return tcPr;
	}

	/**
	 * TODO 得到CTTrPr,不存在则新建
	 * 
	 * @author zhanghan
	 * @date 2016-12-3
	 * 
	 */
	public static CTTrPr getRowCTTrPr(XWPFTableRow row) {
		CTRow ctRow = row.getCtRow();
		CTTrPr trPr = ctRow.isSetTrPr() ? ctRow.getTrPr() : ctRow.addNewTrPr();
		return trPr;
	}
}
